export const BILL_KEY = 'bill'
export const CATEGORY_KEY = 'category'

/**
 * 格式化时间 2020-06-01 12:00:00
 *
 * @param time
 * @returns {{time: string, month: number}}
 */
export const formatTime = (time) => {
  const date = new Date(time)
  const ds = getDateStr(date) + ' '
  const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  return {
    time: ds + h + m + s,
    month: date.getMonth() + 1
  }
}

/**
 * 获取日期字符串
 *
 * @param date
 * @returns {string}
 */
export const getDateStr = (date) => {
  const Y = date.getFullYear() + '-'
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
  return Y + M + D
}

/**
 * 时间字符串转时间戳
 *
 * @param timeStr
 * @returns {number}
 */
export const getTimeInt = (timeStr) => {
  return new Date(timeStr).getTime()
}

/**
 * 保存账单数据至本地
 *
 * @param bills
 */
export const setBills = (bills) => {
  window.localStorage.setItem(BILL_KEY, JSON.stringify(bills))
}

/**
 * 获取账单信息:
 * 1. 返回本地数据
 * 2. 获取初始化数据并保存至本地
 *
 * @returns {Array}
 */
export const getBills = () => {
  const bills = window.localStorage.getItem(BILL_KEY)
  return bills ? JSON.parse(bills) : null
}

/**
 * 保存分类信息到本地
 *
 * @param categories
 */
export const setCategories = (categories) => {
  window.localStorage.setItem(CATEGORY_KEY, JSON.stringify(categories))
}

/**
 * 获取分类信息:
 * 1. 返回本地数据
 * 2. 获取初始化数据并保存至本地
 *
 * @returns {Array}
 */
export const getCategories = () => {
  const categories = window.localStorage.getItem(CATEGORY_KEY)
  return categories ? JSON.parse(categories) : null
}

/**
 * 格式化账单数据
 *
 * @param bills
 * @param categories
 * @returns {Array}
 */
export const formatBills = (bills, categories) => {
  return bills.map((bill) => {
    const t = formatTime(parseInt(bill.time))
    bill.timeStr = t.time
    bill.month = t.month
    bill.type = parseInt(bill.type)
    bill.category = categories.find((category) => {
      if (category.id === bill.category || category.id === bill.category.id) {
        return true
      }
    })
    bill.amount = Math.abs(parseFloat(bill.amount))
    bill.content = (bill.type === 0 ? '支出 ' : '收入 ') + bill.amount.toFixed(2) + ' 元'
    return bill
  })
}

/**
 * 计算收入与支出
 *
 * @param store
 * @param bills
 * @returns {{incomeAmount: string, expendAmount: string}}
 */
export const calcIncomeAndExpendAmount = (store, bills) => {
  let incomeAmount = 0
  let expendAmount = 0
  bills.forEach((bill) => {
    if (bill.type === 1 || bill.type === '1') {
      incomeAmount += bill.amount
    } else {
      expendAmount += bill.amount
    }
  })
  store.commit('setIncomeAmount', { incomeAmount })
  store.commit('setExpendAmount', { expendAmount })
}

export const getMonthList = () => {
  const monthList = []
  for (let i = 1; i <= 12; i++) {
    monthList.push({
      value: i,
      label: i + '月'
    })
  }
  return monthList
}

/**
 * 将账单根据
 *
 * @param bills
 * @param asc
 * @returns {*}
 */
export const sortBills = (bills, asc = true) => {
  return bills.sort((a, b) => asc ? a.amount - b.amount : b.amount - a.amount)
}
